package com.ymatou.socialbizer.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;




import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.socialbizer.parameter.MBuyerShowNoteBean;
import com.ymatou.socialbizer.service.MBuyerShowNoteCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.NoteMongoWapper;

import com.ymttest.utils.YMTDateUtil;

public class Ts_MBuyerShowNote {
	private static MBuyerShowNoteBean mbuyershownoteBean;
	private static MBuyerShowNoteCall mbuyershownoteCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("手机浏览器买家秀笔记接口");
	}

	@Before
	public void caseUp() {
		mbuyershownoteBean=new MBuyerShowNoteBean();
		mbuyershownoteCall=new MBuyerShowNoteCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mbuyershownoteCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_MBuyerShowNote_001() {
		Logger.start(true,"商品买家秀笔记-验证接口返回公共数据 ");
		try {
			//设置参数
			String sproductud = "1ee90f2b-96da-4c77-8606-f0716f44da20";
			mbuyershownoteBean.setFilter("term(sproductid,"+sproductud+")");
			mbuyershownoteBean.setFl("userid,username,pics,noteid,collectionnum,content,addtime,sproductid");
			mbuyershownoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			mbuyershownoteBean.setLimit("0,10");
			
			mbuyershownoteCall.setData(mbuyershownoteBean);
			mbuyershownoteCall.callService();	
			
			JSONArray hits = mbuyershownoteCall.getHits();
			
			Logger.verifyEquals("OK",mbuyershownoteCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(mbuyershownoteCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, mbuyershownoteCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, mbuyershownoteCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(mbuyershownoteCall.getCommonInfo("hostip"), "验证接口返回的服务器ip");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_MBuyerShowNote_002() {
		Logger.start(true,"商品买家秀笔记-验证每条记录数据必返字段是否齐全");
		try {
			//设置参数
			String sproductid = "1ee90f2b-96da-4c77-8606-f0716f44da20";
			mbuyershownoteBean.setFilter("term(sproductid,"+sproductid+")");
			mbuyershownoteBean.setFl("userid,username,pics,noteid,collectionnum,content,addtime,sproductid,commentnum");
			mbuyershownoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			mbuyershownoteBean.setLimit("0,10");
			
			mbuyershownoteCall.setData(mbuyershownoteBean);
			mbuyershownoteCall.callService();	
			
			Date dNow = new Date(); // 当前时间
			
			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
			Date dBefore = calendar1.getTime(); // 得到前一天的时间

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			
			//为防止数据过期验证之前还是先将此sproduct改为满足条件的数据，防止接口无返回数据，很坑。。。
			new NoteMongoWapper().updateDataAddTime(sproductid,format.format(dBefore));
			
			//获取接口返回的数据对象
			JSONArray hits = mbuyershownoteCall.getHits();
			
			for(int i =1;i<=hits.length();i++){
				String ids = hits.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int userid = json.getInt("userid");
				int noteid = json.getInt("noteid");
				int commentnum = json.getInt("commentnum");
				int collectionnum = json.getInt("collectionnum");
				String content = json.getString("content");
				String username = json.getString("username"); 
				JSONArray pics =  (JSONArray) json.get("pics");
				
				Logger.verifyNotNull(userid, "userid-买手id值为必须返回字段值");
				Logger.verifyNotNull(noteid, "noteid-笔记id值为必须返回值");
				Logger.verifyNotNull(commentnum, "commentnum-评论数为必须返回值");
				Logger.verifyNotNull(collectionnum, "collectionnum-点赞数为必须返回值");
				Logger.verifyNotNull(content, "content-笔记内容值为必须返回字段值");
				Logger.verifyNotNull(username, "username-用户名值为必须返回字段值");
				Logger.verifyEquals(true, pics.length()>0, "pics-图片路径为必须返回值");
				
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_MBuyerShowNote_003() {
		Logger.start(true,"商品买家秀笔记-验证是否按照评论数和点赞数排序");
		try {
			//设置参数
			String sproductid = "1ee90f2b-96da-4c77-8606-f0716f44da20";
			mbuyershownoteBean.setFilter("term(sproductid,"+sproductid+")");
			mbuyershownoteBean.setFl("userid,username,pics,noteid,collectionnum,content,addtime,sproductid,commentnum");
			mbuyershownoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			mbuyershownoteBean.setLimit("0,10");
			
			mbuyershownoteCall.setData(mbuyershownoteBean);
			mbuyershownoteCall.callService();	
			
			
			//获取接口返回的数据对象
			JSONArray hits = mbuyershownoteCall.getHits();
			
			for(int i =1;i<hits.length();i++){
				String ids = hits.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				//获取后一条数据
				String twoString =hits.get(i).toString();
				JSONObject twoJsonObject = new JSONObject(twoString);
				
				int commentnum = json.getInt("commentnum");
				int collectionnum = json.getInt("collectionnum");
				
				int twoCommentNum = twoJsonObject.getInt("commentnum");
				int twoCollectionNum = twoJsonObject.getInt("collectionnum");
				
				int total=commentnum+collectionnum;
				int twoTotal = twoCommentNum + collectionnum;
				
				Logger.verifyEquals(true, total>=twoTotal, "第"+i+"条记录应该排在"+i+1+"条前面");
				
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_MBuyerShowNote_004() {
		Logger.start(false,"商品买家秀笔记-当检索商品没有关联订单的笔记接口是否正常");
		try {
			//设置参数
			String sproductid = "1ee90f2b-96da-4c77-8606-f0716f44da21";
			mbuyershownoteBean.setFilter("term(sproductid,"+sproductid+")");
			mbuyershownoteBean.setFl("userid,username,pics,noteid,collectionnum,content,addtime,sproductid,commentnum");
			mbuyershownoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			mbuyershownoteBean.setLimit("0,10");
			
			mbuyershownoteCall.setData(mbuyershownoteBean);
			mbuyershownoteCall.callService();	
			
			//获取接口返回的数据对象
			JSONArray hits = mbuyershownoteCall.getHits();
			
			Logger.verifyEquals(0, mbuyershownoteCall.getTotal(), "返回的total为0");
			Logger.verifyEquals(0, hits.length(), "hits对象为空");
			Logger.verifyEquals("OK", mbuyershownoteCall.getStatus(), "接口状态为ok");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_MBuyerShowNote_005() {
		Logger.start(false,"商品买家秀笔记-商品关联的订单笔记中未通过审核的笔记不会显示出来");
		try {
			//设置参数
			String sproductid = "cddc434a-9767-4fd8-b55e-676383c84a10";
			int noteid = 1001562362;
			mbuyershownoteBean.setFilter("term(sproductid,"+sproductid+")");
			mbuyershownoteBean.setFl("userid,username,pics,noteid,collectionnum,content,addtime,sproductid,commentnum");
			mbuyershownoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			mbuyershownoteBean.setLimit("0,10");
			
			mbuyershownoteCall.setData(mbuyershownoteBean);
			mbuyershownoteCall.callService();	
			
			//获取接口返回的数据对象
			JSONArray hits = mbuyershownoteCall.getHits();
			
			
			Date dNow = new Date(); // 当前时间
			
			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
			Date dBefore = calendar1.getTime(); // 得到前一天的时间

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			
			//为防止数据过期验证之前还是先将此sproduct中的某一笔记状态改为1
			new NoteMongoWapper().updateDataStatus(1,noteid,sproductid);
			
			//检查接口返回数据中是否含有noteid为1001562362的笔记数据
			for(int i =1;i<=hits.length();i++){
				String ids = hits.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteId = json.getInt("noteid");
			
				Logger.verifyNotEquals(noteid, noteId, "未审核通过的关联笔记的商品是不会被显示出来");
				
			}
			
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_MBuyerShowNote_006() {
		Logger.start(true,"商品买家秀笔记-只会返回商品关联订单笔记一个月内的数据");
		try {
			//设置参数
			String sproductid = "cddc434a-9767-4fd8-b55e-676383c84a10";
			
			mbuyershownoteBean.setFilter("term(sproductid,"+sproductid+")");
			mbuyershownoteBean.setFl("userid,username,pics,noteid,collectionnum,content,addtime,sproductid,commentnum");
			mbuyershownoteBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			mbuyershownoteBean.setLimit("0,10");
			
			mbuyershownoteCall.setData(mbuyershownoteBean);
			mbuyershownoteCall.callService();	
			
			//获取接口返回的数据对象
			JSONArray hits = mbuyershownoteCall.getHits();
			
			//设置七天之内的时间
			String end = YMTDateUtil.getDateForMongoFormat(0, 0, 0);
			String start = YMTDateUtil.getDateForMongoFormat(-31, 0, 0);
			
			// 链接mongo库查询数据
			NoteMongoWapper notes = new NoteMongoWapper();
			// 通过mongo获取31天之内的关联订单的已通过审核的笔记
			ArrayList pendNote = notes.getOrderNotesBySproductid(start,end,sproductid);
		
			for(int i=1;i<pendNote.size();i++){
				
			}
			
			
			
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
